package org.gdufe.bookCollections.controller;

import org.gdufe.bookCollections.service.BookCollectionService;
import org.gdufe.pojos.dto.AddCollectionDto;
import org.gdufe.pojos.dto.DeleteCollectionDto;
import org.gdufe.pojos.dto.PageParams;
import org.gdufe.pojos.vo.PageResponseResult;
import org.gdufe.pojos.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/collection")
public class BookCollectionController {
    @Autowired
    private BookCollectionService bookCollectionService;

    //分页查找收藏
    @GetMapping("/querry")
    public PageResponseResult querry(PageParams pageParams){
        Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principalObj.toString();
        return bookCollectionService.querry(pageParams,username);
    }

    //收藏
    @PostMapping("/collect")
    public R collect(@RequestBody AddCollectionDto addCollectionDto){
        Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principalObj.toString();
        return bookCollectionService.addCollection(addCollectionDto,username);
    }

    //取消收藏
    @GetMapping("/cancelCollect")
    public R cancelCollect(Integer bId){
        Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principalObj.toString();
        return bookCollectionService.deleteCollection(bId,username);
    }
}
